#include <stdio.h>
#include <stdlib.h>
#include <time.h>

#define M1 1000
#define N1 10
#define M2 100
#define N2 100
#define M3 10
#define N3 1000
#define T 10000
/*没有cache理论上A应该更快*/
void help_ProgramA_rows(int M, int N) {
    int a[M][N];

    for (int i = 0; i < M; i++)
        for (int j = 0; j < N; j++)
            a[i][j] = i + j;
}
/*没有cache理论上B应该更慢*/
void help_ProgramB_cols(int M, int N) {
    int a[M][N];

    for (int j = 0; j < N; j++)
        for (int i = 0; i < M; i++)
            a[i][j] = i + j;
}

void ProgramA_rows(int M, int N) {
    clock_t execution_time = 0;
    for (int t = 0; t < T; ++t) {
        clock_t start_time = clock();
        help_ProgramA_rows(M, N);
        clock_t end_time = clock();
        execution_time += end_time - start_time;
    }
    printf("Execution time for ProgramA_rows(): %lf microseconds\n", execution_time / (double)T);
}
void ProgramB_cols(int M, int N) {
    clock_t execution_time = 0;
    for (int t = 0; t < T; ++t) {
        clock_t start_time = clock();
        help_ProgramB_cols(M, N);
        clock_t end_time = clock();
        execution_time += end_time - start_time;
    }
    printf("Execution time for ProgramB_cols(): %lf microseconds\n", execution_time / (double)T);
}
int main() {
    int Ms[3] = { M1,M2,M3 };
    int Ns[3] = { N1,N2,N3 };
    for (int i = 0; i < 3; ++i) {
        printf("When M = %d, N = %d:\n", Ms[i], Ns[i]);
        printf("\t");ProgramA_rows(Ms[i], Ns[i]);
        printf("\t");ProgramB_cols(Ms[i], Ns[i]);
    }
    return 0;
}